<%@import=lpg.util.Utility%>
//--<%=control.classFile.getAbsolutePath()%>
//
// DO NOT MODIFY THIS FILE - it is generated using templatec on <%=control.templateFile.getName()%>
//
<%

        if (control.packageOpt != null)
        {
            %>package <%=control.packageOpt%>;

<%
        }

        %>import java.io.PrintWriter;<%
        if (!control.importList.isEmpty())
        {
            for (Control.Element importName : control.importList)
            {
                %>
import <%=importName.content%>; /* <%=importName.fileName%>:<%=importName.endLineNo%> */<%
            }
        }
        %>

<%

        if (control.modifierOpt != null)
        {
            %><%=control.modifierOpt.content%> /* <%=control.modifierOpt.fileName%>:<%=control.modifierOpt.endLineNo%> */ <%
        }

        %>class <%=control.className%><%

        if (control.extendsOpt != null)
        {
            %> extends <%=control.extendsOpt.content%> /* <%=control.extendsOpt.fileName%>:<%=control.extendsOpt.endLineNo%> */<%
        }

        if (!control.implementsList.isEmpty())
        {
            %> implements <%
            for (Control.Element implementsName : control.implementsList)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    %>, <%
                }
                %><%=implementsName.content%> /* <%=implementsName.fileName%>:<%=implementsName.endLineNo%> */<%
            }
        }

        %>
{
    private PrintWriter out = null;

    public void setOut(PrintWriter pw)
    {
        out = pw;
    }
<%
        if (control.mainOpt == null || control.mainOpt.content.length() > 0)
        {
            %>
    <%
            if (control.mainOpt == null)
            {
                %>public void run()<%
            }
            else
            {
                %><%=control.mainOpt.content%> /* <%=control.mainOpt.fileName%>:<%=control.mainOpt.endLineNo%> */<%
            }
            %>
    {
        <%
            first = true;
            for (Control.Element element : control.mainList)
            {
                generateElement(element);
            }
        %>
    }
<%
        }

        for (Control.Element element : control.memberList)
        {
            generateElement(element);
        }
        %>
}
<%!
    private boolean first = true;
    private int indent = 8; // Default indent in run()

    private final Control control;

    %><%@classname%><%(Control ctrl, PrintWriter writer)
    {
        control = ctrl;
        setOut(writer);
    }

    private void generateElement(Control.Element element)
    {
        switch (element.state)
        {
            case ACTION:
                indent();
                first = false;
                %>out.write(<%=Utility.escape(element.content, '"')%>); /* <%=element.fileName%>:<%=element.startLineNo%><%
                if (element.startLineNo != element.endLineNo)
                {
                    %>~<%=element.endLineNo%><%
                }
                %> */<%
                break;
            case EXPRESSION:
                indent();
                first = false;
                %>out.print(<%=element.content%>); /* <%=element.fileName%>:<%=element.endLineNo%> */<%
                break;
            case DECLARATION:
            case SCRIPTLET:
                indent = element.endColumnNo;
                first = true;
                %><%=element.content%><%
                break;
            default:
                assert false;
        }
    }

    // There is no way to format perfectly, just try our best
    private void indent()
    {
        if (!first)
        {
            %>
<%
            for (int i = 0; i < indent; i++)
            {
                %> <%
            }
        }
    }
%>
